package cn.xspace.module.other.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.ServiceConstant;
import com.xspace.android.xafcommon.util.StringUtils;

import cn.xspace.module.other.application.MyApplication;

public class MAFJsPlugin {
	private Context mContext;

	public MAFJsPlugin(Context mContext) {
		this.mContext = mContext;
	}

	public void toActivity() {
		System.out.println("------------------------------------------------action:");
		Log.d("打印测试", "dddd");
	}

	//@JavascriptInterface
	public void toActivity(String action, String name, String key, String url) {
		Log.d("打印测试", action + ":" + name + ":" + key + ":" + url);
		System.out.println("-----------------------------111-------------------action:" + action);
		System.out.println("-----------------------------111-------------------key:" + key);
		if (!MyApplication.get().isAccountConfigured() && !"browser".equals(key) && !"refresh".equals(key)) {
			String cls = "cn.xspace.app.other.activity.LoginAct";
			showConfimLoginDialog(cls, url);
			return;
		}
		if ("native".equals(action)) {
			String cls = null;
			if ("wnx".equals(key)) {
				Log.d("测试", key);
				cls = "cn.xspace.app.wnx.activity.WnxMainAct";
			} else if ("qac".equals(key)) {
				cls = "cn.xspace.app.qac.HomeQACTabHostAct";
			} else if ("law".equals(key)) {
				cls = "cn.xspace.app.law.HomeLawTabHostAct";
			} else if ("appstore".equals(key)) {
				cls = "cn.xspace.app.appstore.HomeAppStoreTabHostAct";
			} else if ("nmg".equals(key)) {
				cls = "cn.xspace.app.nmg.HomeNMGTabHostAct";
			} else if ("gqxx".equals(key)) {
				cls = "cn.xspace.app.spdm.HomeSPDMTabHostAct";
			} else if ("xzl".equals(key)) {
				cls = "cn.xspace.app.sms.activity.SmsMainAct";
			} else if ("setting".equals(key)) {
				cls = "cn.xspace.app.other.activity.SettingAct";
			} else if ("functionCenter".equals(key)) {
				// cls =
				// "cn.xspace.app.appstore.installed.activity.InstalledAct";
				// cls="cn.xspace.app.appstore.installed.activity.TrusteeCenterAct";
				cls = "cn.xspace.app.appstore.installed.activity.InstalledMainAct";

			} else if ("agInfoUnion".equals(key)) {
				cls = "cn.xspace.app.addressbook.AddressbookUnion";
			} else if ("mywork".equals(key)) {//我的工作
				cls = "cn.xspace.app.worklog.activity.MyWorkMainActivity";
			} else if ("refresh".equals(key)) {
				Log.d("MAFJsPlugin", "refresh...");
				Log.d("MAFJsPlugin", "mRefreshHtml==null?..." + (mRefreshHtml == null));
				if (mRefreshHtml != null) {
					mRefreshHtml.refreshCallBack();
				}
			} else if ("browser".equals(key)) {
				cls = "cn.xspace.app.exweb.activity.ExWebAct";
			} else {
				cls = "cn.xspace.app.other.activity.SettingAct";
			}
			if (action.equals("native")) {
				startAct(cls, url);
			} else if (action.equals("webnative")) {

			} else if (action.equals("web")) {

			}
		}
	}

	@JavascriptInterface
	public void toWNXActivity(String action, String type, String key, String url, String pushid, String pushname) {

		System.out.println("-----------------------------333-------------------action:" + action);
		System.out.println("-----------------------------333-------------------key:" + key);
		String tempUrl = null;
		if ("openNavite".equals(action)) {
			String cls = null;
			if ("wnx".equals(key)) {
				cls = "cn.xspace.app.wnx.activity.WnxMainAct";
			} else if ("qac".equals(key)) {
				cls = "cn.xspace.app.qac.HomeQACTabHostAct";
			} else if ("law".equals(key)) {
				cls = "cn.xspace.app.law.HomeLawTabHostAct";
			} else if ("appstore".equals(key)) {
				cls = "cn.xspace.app.appstore.HomeAppStoreTabHostAct";
			} else if ("nmg".equals(key)) {
				cls = "cn.xspace.app.nmg.HomeNMGTabHostAct";
			} else if ("gqxx".equals(key)) {
				cls = "cn.xspace.app.spdm.HomeSPDMTabHostAct";
			} else if ("xzl".equals(key)) {
				cls = "cn.xspace.app.sms.activity.SmsMainAct";
			} else if ("setting".equals(key)) {
				cls = "cn.xspace.app.other.activity.SettingAct";
			} else if ("queryDeptInfo".equals(key)) {
				cls = "cn.xspace.app.wnx.activity.PlatformMessageAct";
			} else if ("functionCenter".equals(key)) {
				// cls =
				// "cn.xspace.app.appstore.installed.activity.InstalledAct";
				// cls="cn.xspace.app.appstore.installed.activity.TrusteeCenterAct";
				cls = "cn.xspace.app.appstore.installed.activity.InstalledMainAct";
			} else if ("browser".equals(key)) {
				cls = "cn.xspace.app.exweb.activity.ExWebAct";
				tempUrl = url;
			} else if ("refresh".equals(key)) {
				if (mRefreshHtml != null) {
					mRefreshHtml.refreshCallBack();
				}
			}

			else {
				cls = "cn.xspace.app.other.activity.SettingAct";
			}
			if (type.equals("native")) {
				//本地应用没有登录时 直接跳到登录页
				if (!MyApplication.get().isAccountConfigured() && !"browser".equals(key) && !"refresh".equals(key)) {
					cls = "cn.xspace.app.other.activity.LoginAct";
					showConfimLoginDialog(cls, tempUrl);
				} else if (key.equals("queryDeptInfo") && cls.indexOf("LoginAct") < 0) {
					toWnxDeptDetailListAct(cls, tempUrl, pushid, pushname);
				} else {
					startAct(cls, tempUrl);
				}
			} else if (type.equals("webnative")) {

			} else if (type.equals("web")) {
			}
		}

	}

	@JavascriptInterface
	public String toActivity(String action, String type, String key, String url, String nativetitle) {
		System.out.println("-----------------------------222-------------------action:" + action);
		System.out.println("-----------------------------222-------------------key:" + key + " ntitle:" + nativetitle);
		String tempUrl = null;
		if ("openNavite".equals(action)) {
			String cls = null;
			if ("wnx".equals(key)) {
				cls = "cn.xspace.app.wnx.activity.WnxMainAct";
			} else if ("qac".equals(key)) {
				cls = "cn.xspace.app.qac.HomeQACTabHostAct";
			} else if ("law".equals(key)) {
				cls = "cn.xspace.app.law.HomeLawTabHostAct";
			} else if ("appstore".equals(key)) {
				cls = "cn.xspace.app.appstore.HomeAppStoreTabHostAct";
			} else if ("nmg".equals(key)) {
				cls = "cn.xspace.app.nmg.HomeNMGTabHostAct";
			} else if ("gqxx".equals(key)) {
				cls = "cn.xspace.app.spdm.HomeSPDMTabHostAct";
			} else if ("xzl".equals(key)) {
				cls = "cn.xspace.app.sms.activity.SmsMainAct";
				//cls =PullParserUtils.getModelClass(key);
			} else if ("setting".equals(key)) {
				cls = "cn.xspace.app.other.activity.SettingAct";
			} else if ("functionCenter".equals(key)) {
				// cls =
				// "cn.xspace.app.appstore.installed.activity.InstalledAct";
				// cls="cn.xspace.app.appstore.installed.activity.TrusteeCenterAct";
				cls = "cn.xspace.app.appstore.installed.activity.InstalledMainAct";
			} else if ("agInfoUnion".equals(key)) {//联盟商圈---通讯录
				cls = "cn.xspace.app.addressbook.AddressbookUnion";
				//cls =PullParserUtils.getModelClass(key);
			} else if ("subscribe".equals(key)) {//订阅
				cls = "cn.xspace.app.h5.SubscriptionManageActivity";
			} else if ("mywork".equals(key)) {//我的工作
				cls = "cn.xspace.app.worklog.activity.MyWorkMainActivity";
			} else if ("browser_build_url".equals(key)) {//需要本地拼装url
				if (checkIsNeedJumpToLogin(key)) {//logoClick
					cls = "cn.xspace.app.other.activity.LoginAct";
					showConfimLoginDialog(cls, tempUrl);
				} else {
					jumpToCordova(url);
				}
				return "";
			} else if ("homepage".equals(key)) {//需要本地拼装url
				if (mRefreshHtml != null) {
					//mRefreshHtml.refreshCallBack();
				}
			} else if ("browser".equals(key) || "browser_phonegap".equals(key)) {
				cls = "cn.xspace.app.exweb.activity.ExWebAct";
				tempUrl = url;
				LogUtil.v("mafJs", "--------tempUrl-------------------:" + tempUrl);
			} else if ("refresh".equals(key)) {
				if (mRefreshHtml != null) {
					mRefreshHtml.refreshCallBack();
				}
			} else {
				cls = "cn.xspace.app.other.activity.SettingAct";
			}
			if (type.equals("native")) {
				//本地应用没有登录时 直接跳到登录页
				if (checkIsNeedJumpToLogin(key)) {//logoClick
					cls = "cn.xspace.app.other.activity.LoginAct";
					showConfimLoginDialog(cls, tempUrl);
				} else {
					startAct_T(cls, tempUrl, nativetitle);
				}
			} else if (type.equals("webnative")) {

			} else if (type.equals("web")) {
			}
			Log.d("测试", cls);
		}
		return "test-test";
	}

	private String strExTitle = "";

	public void setExWebAct(String title) {
		this.strExTitle = title;
	}

	private void startAct(String packageName, String url) {
		if (packageName != null && packageName.length() > 0) {
			try {
				Class activityClass = Class.forName(packageName);
				Intent intent = new Intent(mContext, activityClass);// 你想去的activity(exp:Temp)
				if (!StringUtils.isNull(url)) {
					intent.putExtra(Constants.TITITL, strExTitle);
					intent.putExtra(Constants.WEBVIEURL, url);
				}
				mContext.startActivity(intent);
			} catch (ClassNotFoundException e) {
				Log.e("Why", "错误原因：" + packageName);
				e.printStackTrace();
			}
		}
	}

	/**
	 * 含有nativeTitle的方法
	 * @param packageName
	 * @param url
	 * @param nativeTtile
	 */
	private void startAct_T(String packageName, String url, String nativeTtile) {
		if (packageName != null && packageName.length() > 0) {
			try {
				Class activityClass = Class.forName(packageName);
				Intent intent = new Intent(mContext, activityClass);// 你想去的activity(exp:Temp)
				if (!StringUtils.isNull(url)) {
					intent.putExtra(Constants.TITITL, strExTitle);
					intent.putExtra(Constants.WEBVIEURL, url);
				}
				if (!"undefined".equals(nativeTtile)) {
					intent.putExtra(Constants.NativeTitle, nativeTtile);
				}
				mContext.startActivity(intent);
			} catch (ClassNotFoundException e) {
				Log.e("Why", "错误原因：" + packageName);
				e.printStackTrace();
			}
		}
	}

	private void toWnxDeptDetailListAct(String cls, String url, String pushid, String pushname) {
		if (cls != null && cls.length() > 0) {
			try {
				Class activityClass = Class.forName(cls);
				Intent intent = new Intent(mContext, activityClass);// 你想去的activity(exp:Temp)
				if (!StringUtils.isNull(pushid)) {
					intent.putExtra(ServiceConstant.BR_SERVICE_PUSHMSG_RECEIVED_USERID_KEY, pushid);
					intent.putExtra(ServiceConstant.BR_SERVICE_PUSHMSG_RECEIVED_PLATFORM_KEY, "WNXWEB");
					intent.putExtra(ServiceConstant.BR_SERVICE_PUSHMSG_RECEIVED_TITLE_KEY, pushname);
				}
				mContext.startActivity(intent);
			} catch (ClassNotFoundException e) {
				Log.e("Why", "错误原因：" + cls);
				e.printStackTrace();
			}
		}

	}

	private RefreshHtml mRefreshHtml;

	public void initRefreshHtml(RefreshHtml mRefreshHtml) {
		this.mRefreshHtml = mRefreshHtml;
		Log.d("MAFJsPlugin", "mRefreshHtml==null?..." + (mRefreshHtml == null));
	}

	public interface RefreshHtml {
		public void refreshCallBack();
	}

	public static String noLoginTip = "该应用需要注册登录才能使用,现在注册|登录?";

	private void showConfimLoginDialog(final String packageName, final String url) {
		new AlertDialog.Builder(mContext).setTitle("提示").setMessage(noLoginTip)
				.setPositiveButton("是", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						startAct(packageName, url);

					}
				}).setNegativeButton("否", null).show();

	}

	private boolean checkIsNeedJumpToLogin(String key) {
		if (TextUtils.isEmpty(key))
			return true;
		boolean isneed = true;
		//可不登录直接跳转的key
		String[] filterKey = { "browser", "refresh", "setting", "appstore", "qac", "browser_phonegap" };
		if (!MyApplication.get().isAccountConfigured()) {
			for (int i = 0; i < filterKey.length; i++) {
				if (filterKey[i].equals(key)) {
					isneed = false;
					break;
				}
			}
		} else {
			isneed = false;
		}

		return isneed;

	}

	private void jumpToCordova(String url) {
		if (TextUtils.isEmpty(url))
			return;
//		Intent in0 = new Intent(mContext, MyCordovaWebViewAct.class);
//		in0.putExtra("loadUrl", WorkLogConstant.getMyWorkUrl(url));
//		mContext.startActivity(in0);

	}

	@JavascriptInterface
	public void onLoad() {
		Log.d("MAFJsPlugin", "onLoadCompleted");
		MAFJsPlugin.this.onLoadCompleted();
	}

	public void onLoadCompleted() {
		//webView.loadUrl("full-js-here");
	}

	//	private void initData() {
	//		try {
	//			Intent intent = new Intent();
	//			intent.setClassName("cn.xspace.module.mywork", "cn.xspace.module.mywork.activity.MyWorkMainActivity");
	//			intent.putExtra("packageName", MyApplication.get().getPackageName());
	//			mContext.startActivity(intent);
	//		} catch (Exception e) {
	//			DialogHelp.getMessageDialog(mContext, "首次使用需要安装我的工作模块，确认安装吗？", "提醒", "取消", "确定", new OnClickListener() {
	//
	//				@Override
	//				public void onClick(DialogInterface dialog, int which) {
	//					Toast.makeText(mContext, "这可能需要一小段时间，请耐心等待！", Toast.LENGTH_LONG).show();
	//					new Thread(new Runnable() {
	//
	//						@Override
	//						public void run() {
	//							try {
	//								Thread.sleep(500);
	//							} catch (InterruptedException e) {
	//								e.printStackTrace();
	//							}
	//							Tools.copyAndInstall(mContext, "MAF-Android-Module-MyWork.apk");
	//						}
	//					}).start();
	//
	//				}
	//			}, new OnClickListener() {
	//				@Override
	//				public void onClick(DialogInterface dialog, int which) {
	//				}
	//			}).create().show();
	//			//去assets里面拿子apk,拷贝到项目手机指定的安装目录下，安装子apk。
	//
	//		}
	//	}
}
